package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSDocAction;
import leeon.bbsbrower.models.DocObject;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class DocView extends ViewPart {
	public static final String ID = "bbsbrower.view.doc";

	private Table table = null;
	
	//当前版面的标记
	private static int board = 0;
	
	static String bname = null;
	
	//当前版面进入的url
	private static String url = null;
	
	//当前版面进入的start
	private static String start = null;
	
			
	//文章数据
	static DocObject[] docs = null;
	
	//入口标志，0 版面列表，1 收藏，2 所有版面, 3 文章内容进入
	private static int en = 0;
	
	//一页大小
	public static int size = 18;
	
	//模式
	private static String t = null;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		table = new Table(parent, SWT.READ_ONLY|SWT.BORDER|SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("序号");
		col1.setWidth(100);
		
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("状态");
		col2.setWidth(50);
		
		TableColumn col3 = new TableColumn(table, SWT.NONE);
		col3.setText("作者");
		col3.setWidth(150);
		
		TableColumn col4 = new TableColumn(table, SWT.NONE);
		col4.setText("标题");
		col4.setWidth(500);
		
		TableColumn col5 = new TableColumn(table, SWT.NONE);
		col5.setText("日期");
		col5.setWidth(200);
		
				
		//上下左右的事件
		table.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					if (DocView.en == 1)
						actionToMyBoards();
					else if (DocView.en == 2)
						actionToAllBoards();
					else if (DocView.en == 0)
						actionToBoardList();
					
					e.doit = false;
					
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {
					
					actionContent();
					e.doit = false;
				//上
				} else if (e.keyCode == SWT.ARROW_UP) {
					int dc = table.getSelectionIndex();
					//上翻页
					if (dc == 0) {
						actionPageUp();
						e.doit = false;
					}
					
				//下
				} else if (e.keyCode == SWT.ARROW_DOWN) {
					int dc = table.getSelectionIndex();
					//下翻页
					if (dc == table.getItemCount() - 1) {
						actionPageDown();
						e.doit = false;
					}
				//最后一条
				} else if (e.keyCode == SWT.END) {
					actionEnd();
					e.doit = false;
				//第一条
				} else if (e.keyCode == SWT.HOME) {
					actionHome();
					e.doit = false;
				//刷新
				} else if (e.keyCode == SWT.F5) {
					actionRefresh();
					e.doit = false;
					
				//上翻
				} else if (e.keyCode == SWT.PAGE_DOWN) {
					//actionPageDown();
					//e.doit = false;
				
				//下翻
				} else if (e.keyCode == SWT.PAGE_UP) {
					//actionPageUp();
					//e.doit = false;
				
				//p,切换主题模式
				} else if (e.character == 'p') {
					t = ("true".equals(t)?"false":"true");
					refreshData(-1, null, DocView.url, 0, -1, null);
					e.doit = false;
				
				//发文
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 112) {
					actionDoc();
					e.doit = false;
				
				//跳转
				} else if ("0123456789".indexOf(e.character) != -1) {
					DocNumberDialog dnd = new DocNumberDialog(DocView.this.getViewSite().getShell(), String.valueOf(e.character));
					dnd.open();
					if (dnd.getRet() != null)
						actionGoTo(dnd.getRet());
				}
				
			}

			public void keyReleased(KeyEvent e) {}
			
		});		
	}
	
	
	/**
	 * 回某个BoardList
	 */
	private void actionToBoardList() {
		//跳转回到版面列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		t = null;
		try {
			BoardListView board = (BoardListView)page.showView(BoardListView.ID);
			board.refreshData(-1, null, DocView.board, 3);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 回收藏
	 */
	private void actionToMyBoards() {
		//跳转回收藏版面列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		t = null;
		try {
			MyBoardView board = (MyBoardView)page.showView(MyBoardView.ID);
			board.refreshData(DocView.board);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 回所有版面
	 */
	private void actionToAllBoards() {
		//跳转回所有版面列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		t = null;
		try {
			AllBoardView board = (AllBoardView)page.showView(AllBoardView.ID);
			board.refreshData(DocView.board);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 进入某个文章
	 */
	private void actionContent() {
		//根据文章列表看文章的action
		int dc = table.getSelectionIndex();
		String url = docs[dc].getURL();

		
		//跳转回进入某片文章
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			ConView con = (ConView)page.showView(ConView.ID);
			con.refreshData(dc, url);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	//下翻
	private void actionPageDown() {
		DocObject doc = DocView.docs[0];
		String start = String.valueOf(Integer.parseInt(doc.getNumber()) + DocView.size );
		refreshData(-1, null, DocView.url, 0, -1, start);
	}
	
	//上翻
	private void actionPageUp() {
		DocObject doc = DocView.docs[0];
		String start = String.valueOf(Integer.parseInt(doc.getNumber()) - DocView.size);		
		refreshData(-1, null, DocView.url, DocView.size-1, -1, start);
	}
	
	//下翻
	static void actionToPageDown() {
		DocObject doc = DocView.docs[0];
		String start = String.valueOf(Integer.parseInt(doc.getNumber()) + DocView.size );
		refreshDataNoView(-1, null, DocView.url, 0, -1, start);
	}
	
	//上翻
	static void actionToPageUp() {
		DocObject doc = DocView.docs[0];
		String start = String.valueOf(Integer.parseInt(doc.getNumber()) - DocView.size);		
		refreshDataNoView(-1, null, DocView.url, DocView.size-1, -1, start);
	}
	
	//home
	private void actionHome() {
		refreshData(-1, null, DocView.url, 0, -1, "1");
	}
	
	//end
	private void actionEnd() {
		refreshData(-1, null, DocView.url, DocView.size-1, -1, null);
	}
	
	//f5
	private void actionRefresh() {
		int d = table.getSelectionIndex();
		refreshData(-1, null, DocView.url, d, -1, DocView.start);
	}
	
	//goto
	private void actionGoTo(String index) {
		refreshData(-1, null, DocView.url, 0, -1, index);
	}
	
	//回文
	private void actionDoc() {
		int d = table.getSelectionIndex();
		
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			SendDocView doc = (SendDocView)page.showView(SendDocView.ID);
			doc.refreshData(0, DocView.bname, null, d);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新数据
	 * @param blocks	 
	 */
	public static void refreshDataNoView(int board, String bname, String url, int index, int en, String start) {
		if (url != null) {
			//切换模式
			if (t != null) {
				if ("true".equals(t)) {
					url = url.replaceFirst("bbsdoc", "bbstdoc");
				} else {
					url = url.replaceFirst("bbstdoc", "bbsdoc");
				}
			}
			
			try {
				if (url.indexOf("bbstdoc") != -1)
					DocView.docs = BBSDocAction.getTDocsByURL(url, start);
				else
					DocView.docs = BBSDocAction.getDocsByURL(url, start);
			} catch (Exception e) {
				MessageDialog.openError(Display.getDefault().getActiveShell(), "错误信息", e.toString());
				e.printStackTrace();
			}
			
			DocView.url = url;
			DocView.start = start;
		}		
		
		//如果是-1则不赋值
		if (board != -1) DocView.board = board;
		if (bname != null) DocView.bname = bname;
		if (en != -1) DocView.en = en;
	}
	
	
	public void refreshData(int board, String bname, String url, int index, int en, String start) {
		//提取数据		
		if (url != null) {
			//切换模式
			if (t != null) {
				if ("true".equals(t)) {
					url = url.replaceFirst("bbsdoc", "bbstdoc");
				} else {
					url = url.replaceFirst("bbstdoc", "bbsdoc");
				}
			}
			
			try {
				if (url.indexOf("bbstdoc") != -1) {
					DocView.docs = BBSDocAction.getTDocsByURL(url, start);
				} else {
					DocView.docs = BBSDocAction.getDocsByURL(url, start);
				}
			} catch (Exception e) {
				MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
				e.printStackTrace();
			}
			DocView.url = url;
			DocView.start = start;
		}		
		
		//如果是-1则不赋值
		if (board != -1) DocView.board = board;
		if (bname != null) DocView.bname = bname;
		if (en != -1) DocView.en = en;
		

		table.removeAll();
		boolean show = (DocView.start==null);		
		if (DocView.docs != null) {
			int max = (show)?DocView.docs.length:DocView.size;
			for (int i = 0; i < max; i ++) {
				if ("[∞]".equals(DocView.docs[i].getNumber())) {
					if (!show) continue;
				}
				TableItem it = new TableItem(table, SWT.NONE);
				it.setText(new String[] {
					DocView.docs[i].getNumber(), 
					DocView.docs[i].getStatus(),
					DocView.docs[i].getAuthor(),
					DocView.docs[i].getTitle(),
					DocView.docs[i].getDate()
					});
				it.setData(DocView.docs[i]);
			}
			//选中某个
			table.setSelection(index);
		}
					
		ApplicationActionBarAdvisor.setStatusLine();

	}	
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		table.setFocus();
	}
}